<?php	if (! defined ( 'BASEPATH' ))exit ( 'No direct script access allowed' );

class Login_control extends CI_Controller {
	
	function __construct()
	{
		parent::__construct ();
		$this->load->model('users_model');
		$this->load->model('drugs_model');
	}

  	function index()
  	{
  	$data['main_content'] = 'login_view';
    $this->load->view('includes/template', $data);
  	}
  	
  	public function view_error($error)
  	{
		$data['main_content'] = 'login_view';
		$data['error'] = $error;
    $this->load->view('includes/template', $data);
  	}
  
  	function validate_user() 
  	{
  		
		//$this->load->model('users_model');
		$query = $this->users_model->validate();
		if ($query) { // if user's credentials validated...
			$data = array (
					'username' => $this->input->post ('username'),
					'is_logged_in' => TRUE 
			);
			
			$this->session->set_userdata($data);
			redirect ('site_admin/sale_view');
		} else {
			// $this->index();
			$data ['error'] = 'Invalid Username or Password';
			$data ['main_content'] = 'login_view';
			$this->load->view('includes/template', $data);
		}
	}
	

	public function verifylogin()
	{
		 
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
		//$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
	
		if($this->form_validation->run() == FALSE)
		{
			 
			//  $this->load->view('login_form');
			$error = "Username or Password is invalid";
			/*$error_data = array(
					'error' => $error
			);*/	 
			$this->view_error($error);
			//$this->load->view('home_control/view_error',$error);
			 
			 
		}
		else
		{
			 
			redirect('login_control/home', 'refresh');
		}
	
	}
	
	function check_database($password)
	{
		$username = $this->input->post('username');

		$password = md5($password);

		$result = $this->users_model->login($username,$password);
		if($result)
		{
			$sess_array = array();
			foreach($result as $row)
			{
				$sess_array = array(
						'user_id' => $row->user_id,
						'username' => $row->username,
						'usertype_id'  => $row->usertype_id
				);
				$this->session->set_userdata('logged_in', $sess_array);
			}
			return TRUE;
		}
		else
		{
			$this->form_validation->set_message('check_database', 'Invalid username or password');
			return false;
		}
	}
	
	public function home()
	{
	
		if($this->session->userdata('logged_in')) {
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
		 $data['user_id'] = $session_data['user_id'];
		 $data['usertype_id'] = $session_data['usertype_id'];
		 $id = $session_data['usertype_id'];
	
		 $user = array('data' => $data );
		 	
		 if($data['usertype_id'] == 1) {
			
		 	//$this->load->view('adminsite/sale_view',$user);
			 redirect('sale_ctrl','refresh');
			}
			if($data['usertype_id'] == 2) {
				
				//$this->load->view('usersite/sale_view',$user);
				 redirect('sale_ctrl/sale_user','refresh');
				}
		}
		else {
			redirect('login', 'refresh');
			//$this->view_login($user);
		}
	}
  
   	function logout() {
		$this->session->sess_destroy ();
		redirect('login_control', 'refresh');
	}

}

